package aimis.util;

import com.fasterxml.jackson.core.JsonProcessingException;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtils {
	public static String toDateString(Date date,String patten)
	{
		SimpleDateFormat sd = new SimpleDateFormat(patten);
		return sd.format(date);
	}
	/**
	 * construct a <tt>java.util.Date</tt> object with a string 由 yyyy-MM-dd
	 * HH:mm:ss 格式字符串生成 Date 对象
	 * 
	 * @param s is a string with format of yyyy-MM-dd HH:mm:ss
	 * @return Date object
	 */
	public static Date toDate(String s) {
		return toDate(s,"yyyy-MM-dd HH:mm:ss");
	}
	public static Date toDate(String s,String pattern) {
		if (s == null || "".equals(s)) {
			return null;
		}
		SimpleDateFormat sd = new SimpleDateFormat(pattern);
		Date d;
		try {
			d = sd.parse(s);
			return d;
		} catch (ParseException e) {
			e.printStackTrace();
			return new Date();
		}
	}

	public static Date jsonStringToDate(String s) {
		if (s == null || "".equals(s)) {
			return null;
		}
		s = s.substring(0, 19);
		s = s.replaceAll("T", " ");
		return toDate(s);
	}

	/**
	 * construct a <tt>java.util.Date</tt> object with some parameters 使用年月日时分秒直接生成
	 * Date 对象
	 * 
	 * @param year
	 * @param month
	 * @param date
	 * @param hrs
	 * @param min
	 * @param sec
	 * @return Date object
	 */
	public static Date toDate(int year, int month, int date, int hrs, int min, int sec) {
		StringBuffer dateStr = new StringBuffer();
		dateStr.append(year);
		dateStr.append("-");
		if (month < 10)
			dateStr.append("0");
		dateStr.append(month);
		dateStr.append("-");
		if (date < 10)
			dateStr.append("0");
		dateStr.append(date);
		dateStr.append(" ");
		if (hrs < 10)
			dateStr.append("0");
		dateStr.append(hrs);
		dateStr.append(":");
		if (min < 10)
			dateStr.append("0");
		dateStr.append(min);
		dateStr.append(":");
		if (sec < 10)
			dateStr.append("0");
		dateStr.append(sec);
		return toDate(dateStr.toString());
	}

	/**
	 * Calculate time difference between two Date object in minutes rounded up,计算两个
	 * Date 的时间差，单位分钟，向上取整
	 * 
	 * @param start
	 * @param end
	 * @return minutes between start and end
	 */
	public static Integer diff(Date start, Date end) {
		return (int) Math.ceil((end.getTime() - start.getTime()) / 60000.0);
	}
	
	//获取当前时间
	public static Date getCurrentTime()
	{
		Date date = new Date();
		Date current = DateUtils.toDate(DateUtils.toDateString(date, "yyyy-MM-dd HH:mm:ss"));
		return current;
	}
	// 获取当前时间的后一个月
	public static Date getNextMonth(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.MONTH, 1);
		return calendar.getTime();
	}
	// 获取当前时间的前一个月
	public static Date getPreviousMonth(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.MONTH, -1);
		return calendar.getTime();
	}

//	public static Date toDateFromISOTimeString(String s){
//		SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
//		// 解析日期字符串为 Date 对象
//		Date date = new Date();
//		try {
//			date = inputFormat.parse(s);
//		} catch (ParseException e) {
//			e.printStackTrace();
//		}
//		return date;
//	}

}
